package com.ssh.manager.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.Brand;
import com.ssh.entity.Company;
import com.ssh.entity.Product;
import com.ssh.entity.SuperMarket;
import com.ssh.entity.SuperMarketProduct;
import com.ssh.manager.ProductManager;

@Service
public class ProductManagerImpl implements ProductManager {
	@Resource
	private IDao daoImpl;

	@Override
	public List<Product> getProductsByBrand(Long brandId) {
		String findSql = "from Product where del!=1 and brand.id=?";
		List<Product> products = (List<Product>)daoImpl.findListBySql(findSql, new Object[]{brandId}, false, true, false, 0, -1);
		return products;
	}

	@Override
	public void findProduct(Map conditionMap, Page page, String isDesc) {
		try {
			String sql = "select count(*) from Product where del!=1";
			String findSql = "from Product where del!=1";
			if(conditionMap.get("brand")!=null && !conditionMap.get("brand").equals("")){
				sql += " and brand.id="+conditionMap.get("brand");
				findSql += " and brand.id="+conditionMap.get("brand");
			}
			if (conditionMap.get("productName")!=null && !conditionMap.get("productName").equals("")) {
				sql += " and name like '%"+conditionMap.get("productName")+"%'";
				findSql += " and name like '%"+conditionMap.get("productName")+"%'";
			}
			int productSum = daoImpl.countAll(sql, null);
			page.setTotalCount(new Long(productSum));
			page.countTotalPageNum();
			
			findSql += " order by createDate "+isDesc;
			List<Product> products = (List<Product>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
			page.setPageData(products);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public Product saveOrUpdateProduct(Product product) {
		return daoImpl.saveOrUpdateDomain(product);
	}

	@Override
	public Product getProductById(Long productId) {
		return daoImpl.getDomain(Product.class, productId);
	}
}
